package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService inursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = this.getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        // 批量查询护理项目计划关系
        List<NursingProjectPlan> projectPlans = inursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos = new ArrayList<>();
        projectPlans.forEach(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
            nursingProjectPlanVo.setId(projectPlan.getId());
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(projectPlan.getExecuteFrequency());
            nursingProjectPlanVo.setPlanId(projectPlan.getPlanId());
            nursingProjectPlanVos.add(nursingProjectPlanVo);
        });
        if (!CollectionUtils.isEmpty(projectPlans)) {
            nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        }
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Transactional
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        // 保存护理计划
        // 属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        this.save(nursingPlan);
        if (!CollectionUtils.isEmpty(dto.getProjectPlans())) {
            dto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        }
        // 批量保存护理项目计划关系
        boolean flag = inursingProjectPlanService.saveBatch(dto.getProjectPlans());
        deleteCache();
        return flag ? 1 : 0;
    }
    /** 删除缓存*/
    public void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        if (!CollectionUtils.isEmpty(dto.getProjectPlans())) {
            inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            dto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
            inursingProjectPlanService.saveBatch(dto.getProjectPlans());
        }
        boolean flag = this.updateById(nursingPlan);
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        //删除护理项目计划关系
        inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //删除护理计划
        boolean flag = this.removeById(id);
        deleteCache();
        return flag ? 1 : 0;
    }
    /**
     * 查询所有护理计划
     *
     * @param queryWrapper
     * @return 结果
     */
    @Override
    public List<NursingPlan> listAll(LambdaQueryWrapper<NursingPlan> queryWrapper) {
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (list != null && list.size() > 0) {
            return list;
        }
        list = this.list(queryWrapper);
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
